--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.
--Data Schema For Version 0.0.0.0
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailDelete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailDelete]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailDelete]
(
	@Original_OrderID Int,
	@Original_ProductID Int
)
AS
SET NOCOUNT OFF;

DELETE FROM 
	[Order Details] 
WHERE 
	[OrderID] = @Original_OrderID AND
	[ProductID] = @Original_ProductID ;

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectByOrderDetailPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectByOrderDetailPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE [dbo].[gen_OrderDetailSelectByOrderDetailPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM 
[Order Details]
WHERE

	[Order Details].[OrderID] IN (SELECT [OrderID] 
											FROM OpenXML(@hDoc, '//Order_Details', 2) 
											WITH ([OrderID] char(36) 'OrderID')) 

	AND

	[Order Details].[ProductID] IN (SELECT [ProductID] 
											FROM OpenXML(@hDoc, '//Order_Details', 2) 
											WITH ([ProductID] char(36) 'ProductID')) 


exec sp_xml_removeDocument @hDoc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectByOrderDetailSinglePk]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectByOrderDetailSinglePk]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailSelectByOrderDetailSinglePk]
(
	@OrderID Int,
	@ProductID Int
)
AS

SELECT 
	[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM
[Order Details]
WHERE 
[Order Details].[OrderID] = @OrderID AND [Order Details].[ProductID] = @ProductID 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailInsert]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailInsert]
(
	@UnitPrice Money = default,
	@Quantity SmallInt = default,
	@ProductID Int = default,
	@OrderID Int = default,
	@Discount Real = default,
	@created_date datetime,
	@created_by varchar (50),
	@modified_by varchar (50)

)
AS
SET NOCOUNT OFF;

if (@created_date IS NULL)
SET @created_date = GetDate()

INSERT INTO [Order Details]
(
[Discount],
[OrderID],
[ProductID],
[Quantity],
[UnitPrice],
[created_date],
[created_by],
[modified_date],
[modified_by]

)
VALUES
(
	@Discount,
	@OrderID,
	@ProductID,
	@Quantity,
	@UnitPrice,
	@created_date,
	@created_by,
	@created_date,
	@modified_by

);


SELECT 
	[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM
[Order Details]
WHERE
	[Order Details].[OrderID] = @OrderID AND
	[Order Details].[ProductID] = @ProductID;
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailPagingSelect]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailPagingSelect]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailPagingSelect]
(
	@page int, -- page number selected by the user
	@pageSize int, -- number of items on the page
	@orderByColumn varchar(100), -- name of column to order things by
	@ascending bit, -- order column ascending or descending
	@filter varchar(100) = null, -- filter statement passed in to determine like criteria on order by column
	@count int out -- number of items in the collection
)
AS

SET NOCOUNT ON;


CREATE TABLE #tmpTable
(
[OrderID] Int,
[ProductID] Int
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@pageSize * @page)
IF (@total__ivqatedr <> 0)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[orderid],
	[productid]
)
SELECT
	[Order Details].[orderid],
	[Order Details].[productid]
FROM
[Order Details]
WHERE
	(@orderByColumn = 'Quantity' and (((@filter is null) or ([Order Details].[Quantity] is null)) or (@filter is not null and [Order Details].[Quantity] = @filter)))
or
	(@orderByColumn = 'ProductID' and (((@filter is null) or ([Order Details].[ProductID] is null)) or (@filter is not null and [Order Details].[ProductID] = @filter)))
or
	(@orderByColumn = 'OrderID' and (((@filter is null) or ([Order Details].[OrderID] is null)) or (@filter is not null and [Order Details].[OrderID] = @filter)))
or
	(@orderByColumn = 'Discount' and (((@filter is null) or ([Order Details].[Discount] is null)) or (@filter is not null and [Order Details].[Discount] = @filter)))
ORDER BY
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END

-- set @count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @count = (
SELECT count(*)
FROM
[Order Details]
WHERE
	(@orderByColumn = 'Quantity' and (((@filter is null) or ([Order Details].[Quantity] is null)) or (@filter is not null and [Order Details].[Quantity] = @filter)))
or
	(@orderByColumn = 'ProductID' and (((@filter is null) or ([Order Details].[ProductID] is null)) or (@filter is not null and [Order Details].[ProductID] = @filter)))
or
	(@orderByColumn = 'OrderID' and (((@filter is null) or ([Order Details].[OrderID] is null)) or (@filter is not null and [Order Details].[OrderID] = @filter)))
or
	(@orderByColumn = 'Discount' and (((@filter is null) or ([Order Details].[Discount] is null)) or (@filter is not null and [Order Details].[Discount] = @filter)))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@pageSize * @page) - @pageSize
IF (@total__ivqatedr <> 0)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
SET ROWCOUNT @pageSize
SELECT
	[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [Order Details] ON #tmpTable.[orderid] = [ORDER DETAILS].[orderid] AND
#tmpTable.[productid] = [ORDER DETAILS].[productid]
ORDER BY
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END

DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectBySearchAnd]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectBySearchAnd]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailSelectBySearchAnd]
(
	@Quantity SmallInt = null, 
	@ProductID Int = null, 
	@OrderID Int = null, 
	@Discount Real = null, 
	@paging_PageIndex int = -1, -- page number selected by the user
	@paging_RecordsperPage int = -1, -- number of items on the page
	@paging_OrderByColumn varchar(100) = '', -- name of column to order things by
	@paging_Ascending bit = 1, -- order column ascending or descending
	@paging_Count int out -- number of items in the collection
)
AS

CREATE TABLE #tmpTable 
(
	[OrderID] Int,
[ProductID] Int
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex)
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[orderid],
	[productid]
)
SELECT
	[Order Details].[orderid],
	[Order Details].[productid]
FROM
[Order Details]
WHERE
((@Quantity is null) OR ([Order Details].[Quantity] = @Quantity)) AND
((@ProductID is null) OR ([Order Details].[ProductID] = @ProductID)) AND
((@OrderID is null) OR ([Order Details].[OrderID] = @OrderID)) AND
((@Discount is null) OR ([Order Details].[Discount] = @Discount))
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END
-- set @paging_Count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @paging_Count = (
SELECT count(*)
FROM
[Order Details]
WHERE
((@Quantity is null) OR ([Order Details].[Quantity] = @Quantity)) AND
((@ProductID is null) OR ([Order Details].[ProductID] = @ProductID)) AND
((@OrderID is null) OR ([Order Details].[OrderID] = @OrderID)) AND
((@Discount is null) OR ([Order Details].[Discount] = @Discount))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex) - @paging_RecordsperPage
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
IF (@paging_PageIndex <> -1)
BEGIN
SET ROWCOUNT @paging_RecordsperPage
END
SELECT
	[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [Order Details] ON #tmpTable.[orderid] = [ORDER DETAILS].[orderid] AND
#tmpTable.[productid] = [ORDER DETAILS].[productid]
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END
DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailUpdate]
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailUpdate]
(
	@UnitPrice Money,
	@Quantity SmallInt,
	@Discount Real,
	@modified_by varchar (50),
	@modified_date datetime = null,
	@Original_OrderID Int,
	@Original_ProductID Int,
	@Original_time_stamp timestamp

)
AS

IF (@modified_date IS NULL)
SET @modified_date = GetDate();

SET NOCOUNT OFF;
UPDATE 
[Order Details] 
SET
[Discount] = @Discount,
[Quantity] = @Quantity,
[UnitPrice] = @UnitPrice,
[modified_by] = @modified_by,
[modified_date] = @modified_date

WHERE
	[Order Details].[OrderID] = @Original_OrderID AND
	[Order Details].[ProductID] = @Original_ProductID AND
	[Order Details].[time_stamp] = @Original_time_stamp


SELECT
	[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]
FROM 
[Order Details]
WHERE
	[Order Details].[OrderID] = @Original_OrderID AND
	[Order Details].[ProductID] = @Original_ProductID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectByCreatedDateRange]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectByCreatedDateRange]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailSelectByCreatedDateRange]
(
	@start_date datetime,
	@end_date datetime
)
AS

SET NOCOUNT ON;

SELECT
		[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM
[Order Details]
WHERE
((([Order Details].[created_date] IS NULL) AND (@start_date IS NULL)) OR (@start_date <= [Order Details].[created_date])) AND 
((([Order Details].[created_date] IS NULL) AND (@end_date IS NULL)) OR (@end_date >= [Order Details].[created_date]))
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectByModifiedDateRange]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectByModifiedDateRange]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailSelectByModifiedDateRange]
(
	@start_date datetime,
	@end_date datetime
)
AS

SET NOCOUNT ON;

SELECT
		[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM
[Order Details]
WHERE
((([Order Details].[modified_date] IS NULL) AND (@start_date IS NULL)) OR (@start_date <= [Order Details].[modified_date])) AND 
((([Order Details].[modified_date] IS NULL) AND (@end_date IS NULL)) OR (@end_date >= [Order Details].[modified_date]))
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectBySearchOr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectBySearchOr]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailSelectBySearchOr]
(
	@Quantity SmallInt = null, 
	@ProductID Int = null, 
	@OrderID Int = null, 
	@Discount Real = null, 
	@paging_PageIndex int = -1, -- page number selected by the user
	@paging_RecordsperPage int = -1, -- number of items on the page
	@paging_OrderByColumn varchar(100) = '', -- name of column to order things by
	@paging_Ascending bit = 1, -- order column ascending or descending
	@paging_Count int out -- number of items in the collection
)
AS

CREATE TABLE #tmpTable 
(
	[OrderID] Int,
[ProductID] Int
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex)
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[orderid],
	[productid]
)
SELECT
	[Order Details].[orderid],
	[Order Details].[productid]
FROM
[Order Details]
WHERE
((@Quantity is null) OR ([Order Details].[Quantity] = @Quantity)) OR
((@ProductID is null) OR ([Order Details].[ProductID] = @ProductID)) OR
((@OrderID is null) OR ([Order Details].[OrderID] = @OrderID)) OR
((@Discount is null) OR ([Order Details].[Discount] = @Discount))
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END
-- set @paging_Count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @paging_Count = (
SELECT count(*)
FROM
[Order Details]
WHERE
((@Quantity is null) OR ([Order Details].[Quantity] = @Quantity)) OR
((@ProductID is null) OR ([Order Details].[ProductID] = @ProductID)) OR
((@OrderID is null) OR ([Order Details].[OrderID] = @OrderID)) OR
((@Discount is null) OR ([Order Details].[Discount] = @Discount))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex) - @paging_RecordsperPage
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
IF (@paging_PageIndex <> -1)
BEGIN
SET ROWCOUNT @paging_RecordsperPage
END
SELECT
	[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [Order Details] ON #tmpTable.[orderid] = [ORDER DETAILS].[orderid] AND
#tmpTable.[productid] = [ORDER DETAILS].[productid]
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Quantity' THEN [Order Details].[Quantity] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'ProductID' THEN [Order Details].[ProductID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'OrderID' THEN [Order Details].[OrderID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'Discount' THEN [Order Details].[Discount] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Order Details].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Order Details].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Order Details].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Order Details].[modified_date] END END
DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelect]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelect]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_OrderDetailSelect]
AS

SET NOCOUNT ON;

SELECT
		[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM 
[Order Details]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectByOrderPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectByOrderPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE PROCEDURE [dbo].[gen_OrderDetailSelectByOrderPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM 
[Order Details]
WHERE
	[Order Details].[OrderID] IN (SELECT [OrderID]
				FROM OpenXML(@hDoc, '//Orders', 2)
				WITH ([OrderID] Int 'OrderID'))

exec sp_xml_removeDocument @hDoc	

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OrderDetailSelectByProductPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OrderDetailSelectByProductPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE PROCEDURE [dbo].[gen_OrderDetailSelectByProductPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[Order Details].[unitprice],
	[Order Details].[quantity],
	[Order Details].[productid],
	[Order Details].[orderid],
	[Order Details].[discount],
	[Order Details].[created_by],
	[Order Details].[created_date],
	[Order Details].[modified_by],
	[Order Details].[modified_date],
	[Order Details].[time_stamp]

FROM 
[Order Details]
WHERE
	[Order Details].[ProductID] IN (SELECT [ProductID]
				FROM OpenXML(@hDoc, '//Products', 2)
				WITH ([ProductID] Int 'ProductID'))

exec sp_xml_removeDocument @hDoc	

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

